<template>
  <lay-container :fluid="true" style="padding: 10px;padding-top: 10px;">
    <lay-card>
      <lay-row>
        <lay-col :md="24">
          <lay-form :model="queryModel" ref="layFormRef">
            <lay-row>
              <lay-col :md="4">
                <lay-input size="sm" v-model="queryModel.name" style="width: 90%" placeholder="名称查询"></lay-input>
              </lay-col>
              <lay-col :md="4">
                <lay-input size="sm" v-model="queryModel.code" style="width: 90%" placeholder="编码查询"></lay-input>
              </lay-col>
              <lay-col :md="8">
                <lay-form-item label-width="0">
                  <lay-button size="sm" type="primary" @click="GetList(queryModel)" prefix-icon="layui-icon-search">查询
                  </lay-button>
                  <lay-button size="sm" @click="reset">重置</lay-button>
                  <lay-button size="sm" type="normal" @click="add()" v-permission="PerKey.Dict_AddOrEditView"
                    prefix-icon="layui-icon-addition">新增
                  </lay-button>
                </lay-form-item>
              </lay-col>
            </lay-row>
          </lay-form>
        </lay-col>
        <lay-col :md="24">
          <lay-table :page="page" :columns="columns" :dataSource="dataSource" v-model:selectedKeys="selectedKeys"
            @change="change" :size="tablesize">
            <template v-slot:type="{ data }">
              <lay-space size="xs">
                <lay-badge v-if="data.type">内置</lay-badge>
                <lay-badge theme="green" v-else>普通</lay-badge>
              </lay-space>
            </template>
            <template v-slot:status="{ data }">
              <lay-space size="xs">
                <lay-badge theme="blue" v-if="data.status">启用</lay-badge>
                <lay-badge theme="gray" v-else>禁用</lay-badge>
              </lay-space>
            </template>
            <template v-slot:operator="{ data }">
              <lay-button-group>
                <lay-button type="primary" size="sm" @click="edit(data.id)"
                  v-permission="PerKey.ConfigParm_AddOrEditView" prefix-icon="layui-icon-edit">
                  编辑
                </lay-button>
                <span v-if="!data.type">
                  <lay-button type="danger" size="sm" @click="del(data)" v-permission="PerKey.ConfigParm_Delete"
                    prefix-icon="layui-icon-delete">删除
                  </lay-button>
                </span>
              </lay-button-group>
            </template>
          </lay-table>
        </lay-col>
      </lay-row>
    </lay-card>

    <lay-layer :title="addoredittip" v-model="modelVisible" :resize="true" :area="['800px', '600px']">
      <lay-card style="padding:10px;">
        <lay-row space="10">
          <lay-form :model="form">
            <lay-col :md="24">
              <lay-col md="24">
                <lay-col md="12">
                  <lay-form-item label="类型" prop="type">
                    <lay-radio v-model="form.type" name="action" :value="true" label="内置"></lay-radio>
                    <lay-radio v-model="form.type" name="action" :value="false" label="普通"></lay-radio>
                  </lay-form-item>
                </lay-col>
                <lay-col md="12">
                  <lay-form-item label="编码" prop="code" required required-error-message="类别代码不能空且唯一">
                    <lay-input v-model="form.code"></lay-input>
                  </lay-form-item>
                </lay-col>
              </lay-col>
              <lay-col md="24">
                <lay-form-item label="名称" prop="name" required required-error-message="类别名称不能空">
                  <lay-input v-model="form.name"></lay-input>
                </lay-form-item>
              </lay-col>
              <lay-col md="24">
                <lay-col md="5">
                  <lay-form-item label="参数模板" prop="valueModel">
                    <lay-select v-model="valueModel">
                      <template #append>选择模板</template>
                      <lay-select-option value="string" label="通用"></lay-select-option>
                      <lay-select-option value="uploadImg" label="上传图片"></lay-select-option>
                      <lay-select-option value="date" label="日期"></lay-select-option>
                      <lay-select-option value="switch" label="开关"></lay-select-option>
                    </lay-select>
                  </lay-form-item>
                </lay-col>
                <lay-col md="24">
                  <div v-if="valueModel === 'string'">
                    <lay-form-item label="参数值" prop="value" required required-error-message="参数值不能空">
                      <lay-input v-model="form.value"></lay-input>
                    </lay-form-item>
                  </div>
                  <div v-if="valueModel === 'uploadImg'">
                    <lay-col md="24">
                      <UploadImg v-model="form.value" uplabel="参数值" :file-size="500">
                      </UploadImg>
                    </lay-col>
                  </div>
                  <div v-if="valueModel === 'date'">
                    <lay-form-item label="参数值" prop="value" required required-error-message="参数值不能空">
                      <lay-date-picker v-model="form.value" simple type="date"></lay-date-picker>
                    </lay-form-item>
                  </div>
                  <div v-if="valueModel === 'switch'">
                    <lay-form-item label="参数值" prop="value" required required-error-message="参数值不能空">
                      <lay-radio v-model="form.value" name="action" value="true" label="打开"></lay-radio>
                      <lay-radio v-model="form.value" name="action" value="false" label="关闭"></lay-radio>
                    </lay-form-item>
                  </div>
                </lay-col>
              </lay-col>
              <lay-form-item label="备注" prop="url">
                <lay-col md="24">
                  <lay-input v-model="form.remark"></lay-input>
                </lay-col>
              </lay-form-item>
              <lay-form-item label="状态" prop="status">
                <lay-col md="6">
                  <lay-switch v-model="form.status" onswitch-text="启用" unswitch-text="禁用"></lay-switch>
                </lay-col>
                <lay-col md="6" class="tip"> </lay-col>
              </lay-form-item>
              <lay-col :md="24" class="saveBtn">
                <lay-button @click="submitClick" type="primary" v-permission="PerKey.ConfigParm_AddOrEdit"
                  prefix-icon="layui-icon-ok">
                  {{ addoredittip }}
                </lay-button>
              </lay-col>
            </lay-col>
          </lay-form>
        </lay-row>
      </lay-card>
    </lay-layer>
  </lay-container>
</template>
  
<script lang="ts" setup>
import { reactive, ref, toRefs } from "vue";
import { layer } from "@layui/layer-vue";
import { getList, delOne, getEdit, postAdd, putEdit } from "../../../api/module/config";
import { PerKey } from "../../../library/permissionConst"
import { ConfigParm } from '../../../types/config'
import UploadImg from '../../../layouts/Upload/UploadImg.vue'

const selectedKeys = ref(["1"]);
const page = ref({ total: 0, limit: 15, current: 1, showCount: true, showRefresh: true, limits: [10, 15, 20, 50, 100, 200] });
const tablesize = ref('md');
const valueModel = ref('string')
const columns = [
  { title: "Id", width: "50px", key: "id", align: "center", },
  { title: "类型", customSlot: "type", width: "60px", align: "center", },
  { title: "名称", width: "200px", key: "name", align: "center", },
  { title: "编码", width: "240px", key: "code", },
  { title: "参数", key: "value", width: "380px", align: "center", },
  { title: "备注", key: "remark", },
  { title: "状态", customSlot: "status", width: "60px", align: "center", },
  { title: "操作", width: "200px", customSlot: "operator", key: "operator", align: "left", }
]
const dataSource = ref([])
//定义模型
const putData = reactive({
  queryModel: {
    pageNum: page.value.current,
    pageSize: page.value.limit,
    name: "",
    code: "",
  }
})
const { queryModel } = toRefs(putData)
const cateList = ref({ targetList: [] as any, })
//获取列表
const GetList = function (query: any) {
  if (query != null) {
    queryModel.value.name = query.name
    queryModel.value.code = query.code
  }
  getList(queryModel.value).then((res) => {
    if (res.status == 1) {
      dataSource.value = res.data.data
      cateList.value = res.data.cateList
      page.value.total = res.data.count
    }
  })
}

GetList(queryModel.value)

const layFormRef = ref();
const reset = function () {
  layFormRef.value.reset();
}

const change = function ({ current, limit }: any) {
  queryModel.value.pageNum = current
  queryModel.value.pageSize = limit
  GetList(queryModel.value);
};

const edit = function (id: number) {
  changeVisible(id)
}
const add = function () {
  changeVisible(0)
}

//添加或者编辑
const cids = ref({ targetList: [] as any, })

const modelVisible = ref(false)
const data = reactive({
  form: <ConfigParm><unknown>[]
})

const { form } = toRefs(data)

let addoredittip = ref();

const changeVisible = function (id: number) {
  modelVisible.value = !modelVisible.value
  if (id == 0) {
    addoredittip.value = "新增";
    getEdit(id).then((res) => {
      cids.value = res.data.cateList
      form.value = {
        id: 0,
        type: false,
        code: "",
        name: "",
        value: "",
        remark: "",
        status: true
      }
    })
    return
  } else {
    addoredittip.value = "编辑";
    getEdit(id).then((res) => {
      cids.value = res.data.cateList
      if (res.status == 1) {
        form.value = res.data.data
      }
    })
    return
  }
}

const submitClick = function () {
  if (!form.value.name || !form.value.code || !form.value.value) {
    layer.msg("必填写内容不能空")
    return
  }
  if (form.value.id == 0) {
    postAdd(form.value).then((res) => {
      done(res)
    });
  } else {
    putEdit(form.value).then((res) => {
      done(res)
    });
  }
};
const done = (res: any) => {
  if (res.status == 1) {
    layer.msg(res.msg, () => { });
    modelVisible.value = !modelVisible.value
    GetList(queryModel.value)
  } else {
    layer.msg(res.msg, { icon: 2 });
    return false;
  }
}

//删除
const del = function (data: any) {
  layer.confirm("确认删除ID: " + data.id + " " + data.name + "  参数吗？", {
    title: "警告", move: "true", shade: "false", shadeClose: "true",
    btn: [{
      text: '确认', callback: (layerId: number) => {
        delOne(data.id).then((res) => {
          if (res.status == 1) {
            layer.msg(res.msg, { time: 1000, icon: 1 }, () => {
              GetList(queryModel.value)
            });
          } else {
            layer.msg(res.msg);
          }
          layer.close(layerId);
        })
      }
    }, {
      text: '取消', callback: function (layerId: number) {
        layer.close(layerId);
      }
    }]
  })
}

</script>
<style scoped>

</style>